package com.pe.pgn.clubpgn.webapp.controller.vehiculo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.ClpbTipoVehiculo;
import com.pe.pgn.clubpgn.domain.ClpbVehiculo;
import com.pe.pgn.clubpgn.service.TipoVehiculoService;
import com.pe.pgn.clubpgn.service.VehiculoService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/vehiculo/**")
public class VehiculoController extends BaseFormController{

	@Autowired
	private VehiculoService vehiculoService;
	
	@Autowired
	private TipoVehiculoService tipoVehiculoService;
	
	@RequestMapping(value = "/vehiculolist.html")
    public @ModelAttribute("vehiculoform") ClpbVehiculo listarVehiculo(
    	   @ModelAttribute("vehiculoform") ClpbVehiculo bnvehiculo, 
           @RequestParam(value = "search", required = false) String search, Model model) {

		List<ClpbTipoVehiculo> tiposvehiculos = tipoVehiculoService.obtenerListaTiposVehiculo();
		
		if(StringUtils.isBlank(search)){
			
			List<ClpbVehiculo> vehiculos = vehiculoService.obtenerVehiculos();
			model.addAttribute("vehiculos", vehiculos);
		}else{
			
			List<ClpbVehiculo> vehiculos = vehiculoService.buscarVehiculos(bnvehiculo);
			model.addAttribute("vehiculos", vehiculos);
		}
		
		model.addAttribute("tiposvehiculos", tiposvehiculos);
		return bnvehiculo;
    }
	
	@RequestMapping(value = "/vehiculoform.html", method = RequestMethod.GET)
	public @ModelAttribute("vehiculoform") ClpbVehiculo verVehiculo(
		   @RequestParam(value = "id", required = false) Long id, Model model) {
		
		ClpbVehiculo bnvehiculo = new ClpbVehiculo();
		
		try{
		
			List<ClpbTipoVehiculo> tiposvehiculos = tipoVehiculoService.obtenerListaTiposVehiculo();
			model.addAttribute("tiposvehiculos", tiposvehiculos);
			
			if (id != null) { //modo update			
				bnvehiculo = vehiculoService.obtenerVehiculo(id);
			}
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		model.addAttribute("vehiculoform" , bnvehiculo);		
		return bnvehiculo;
	}
	
	@RequestMapping(value = "/vehiculoform.html", method = RequestMethod.POST)
	public String guardarVehiculo(@ModelAttribute("vehiculoform") ClpbVehiculo bnvehiculo,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {

			if (result.hasErrors()) {
				
				if(bnvehiculo.getId() == null){
					List<ClpbTipoVehiculo> tiposvehiculos = tipoVehiculoService.obtenerListaTiposVehiculo();
					model.addAttribute("tiposvehiculos", tiposvehiculos);
				}
				
				model.addAttribute("vehiculoform", bnvehiculo);
				return "/vehiculo/vehiculoform";
			}

			vehiculoService.guardarVehiculo(bnvehiculo);
			saveMessage(request, getText("vehiculoform.added", request.getLocale()));
			return "redirect:vehiculolist.html";
			
		} catch (Exception e) {
			
			if(bnvehiculo.getId() == null){
				List<ClpbTipoVehiculo> tiposvehiculos = tipoVehiculoService.obtenerListaTiposVehiculo();
				model.addAttribute("tiposvehiculos", tiposvehiculos);
			}
			
			saveError(request, getText("errors.general", request.getLocale()));
			model.addAttribute("vehiculoform", bnvehiculo);
			return "/vehiculo/vehiculoform";
		}
	}
	
	@RequestMapping(value = "/eliminarvehiculo.html")
	public String eliminarVehiculo(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request, Model model) {

		try {

			vehiculoService.eliminarVehiculo(id);
			saveMessage(request, getText("vehiculoform.deleted", request.getLocale()));
			return "redirect:vehiculolist.html";
			
		} catch (Exception e) {
			ClpbVehiculo bnvehiculo = vehiculoService.obtenerVehiculo(id);
			model.addAttribute("vehiculoform" , bnvehiculo);
			saveError(request, getText("errors.general", request.getLocale()));
			return "/vehiculo/vehiculoform";
		}

	}
}
